package data

type UserData struct {
	Id     int    `json:"id"`
	Status string `json:"status"`
}

// 全局变量的map需要手动初始化，否则会runtime panic，因其默认值为nil
var BulbStatus = make(map[int]string) // 存储id对应灯泡的状态，对应red green off

func OnlineCount() int {
	return len(BulbStatus)
}

func AddUserBulb(uid int) {
	lock()
	_, exist := BulbStatus[uid]
	if !exist {
		BulbStatus[uid] = "off"
	}
	unlock()
}

func RemoveUser(uid int) {
	lock()
	delete(BulbStatus, uid)
	unlock()
}

func SetUserBulb(uid int, status string) {
	lock()
	BulbStatus[uid] = status
	unlock()
}

func TurnOffAllLights() {
	lock()
	for k, _ := range BulbStatus {
		BulbStatus[k] = "off"
	}
	unlock()
}

func GetAllUserData() []UserData {
	var data []UserData
	for i, s := range BulbStatus {
		data = append(data, UserData{Id: i, Status: s})
	}
	//fmt.Println(data)
	return data
}
